<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

use itbdw\Ip\IpLocation as IP;
use Rhilip\Ipv6Wry\IpLocation;

class Site_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="产品库分类";
        $this->tablename="sites";  
    }
        
    public function getpage($page,$key="",$sitetype=0,$trash=0){    
        $where=[];
        if($sitetype>0)
        {
            $where["sitetype"]=$sitetype;
        }
        if($trash>0)
        {
            $where["issync"]=0;
        }
        else
        {
            $where["issync"]=1;
        }

        if($key)
        {
            $skey=[
                "name"=>$key,
                "memo"=>$key,
                "domian"=>$key,
            ];
        }
        else
        {
            $skey="";
        }

        return $this->get_items_by_page($where,$page,"backend/sites/site","id desc",[],$skey);                
    }

    public function getsitetype($sitetype)
    {
        $where=["sitetype"=>$sitetype,"issync"=>1];
        return $this->get_items_by_where($where); 
    }

    public function addgcount($siteid)
    {
        $this->auto_add("gcount",["id"=>$siteid]);
    }

    public function addocount($siteid)
    {
        $this->auto_add("ocount",["id"=>$siteid]);
    }

    public function getall(){
        
        $where=[];

        return $this->get_items_by_where($where);                
    }

    public function byserver($serverid)
    {
        $where=["issync"=>1,"serverid"=>$serverid];
        return $this->get_items_by_where($where);        
    }

    public function getallsync()
    {
        $where=["issync"=>1];
        return $this->get_items_by_where($where);        

    }
    public function delete($id)
    {
        
        return true;
    }

    public function getitembyid($id){
        $where=array("id"=>$id);
  
        return $this->get_item_by_where($where);
    }


    public function get_name_by_id($id)
    {
        $where["id"]=$id;
        return $this->get_name_by_where($where);
    }


    public function get_name_index()
    {
        $index=[];
        $sites=$this->getall();
        if($sites)
        {   
            foreach($sites as $site)
            {
                $index[$site["id"]]=$site["name"];
            }

        }

        return $index;
    }


    public function get_url_index()
    {
        $index=[];
        $sites=$this->getall();
        if($sites)
        {   
            foreach($sites as $site)
            {
                $index[$site["id"]]=$site["domian"];
            }

        }

        return $index;
    }
    public function create($data){
        if(!isset($data["addtime"])){
            $data["addtime"]=date("Y-m-d H:i:s");
        }
       
        return $this->add_item($data);       
    }

    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
           
        }else{
            $where=array("id"=>$id);
           
        }
        return $this->update_item($where,$data);

    }

    public function get_ipv4($ip_addr)
    {
        $ip = IP::getLocation($ip_addr);
        return $ip;
    }
    public function get_ipv6($ip_addr)
    {
        $ip = IpLocation::searchIp($ip_addr);
        $ip_addr = explode(" ", $ip['area'])[0];
        return $ip_addr;
    }
}